import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1.构造 DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bite?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.构造SQL
        String sql = "select * from people";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行SQL
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.print("id ->: " + id);
            System.out.println("  name ->: " + name);
        }

        //5.释放必要资源
        statement.close();
        connection.close();

    }
}
